<?php

namespace app\admin\controller\projects\content;

use app\Base;
use app\service\admin\projects\content\ClassifyService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
use think\response\View;

/**
 * 分类
 * Class Classify
 * @package projects\controller\admin\content
 */
class Classify extends Base
{

    /**
     * 页面
     * @return View
     */
    public function show(): View
    {
        return view('projects/content/classify');
    }

    /**
     * @param ClassifyService $service
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getList(ClassifyService $service): Json
    {
        $res = $service::getList($this->params);
        $this->res['count'] = $res['count'];
        $this->res['data'] = $res['data'];
        return $this->returnData();
    }

    /**
     * 添加
     * @param ClassifyService $service
     * @return Json
     */
    public function add(ClassifyService $service): Json
    {
        $res = $service::add($this->params);
        if (!$res) {
            return $this->returnData('addFail');
        }
        return $this->returnData();
    }

    /**
     * 修改
     * @param ClassifyService $service
     * @return Json
     * @throws DbException
     */
    public function update(ClassifyService $service): Json
    {
        $res = $service::update($this->params);
        if (!$res) {
            return $this->returnData('updateFail');
        }
        return $this->returnData();
    }

    /**
     * 删除
     * @param ClassifyService $service
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function delete(ClassifyService $service): Json
    {
        $res = $service::delete($this->params);
        switch (true) {
            case !$res:
                return $this->returnData('deleteFail');
            case is_string($res):
                return $this->returnData('fail', $res);
            default:
                return $this->returnData();
        }
    }
}